<?
/* Configuration Manager v1.1
Made by: Are Haugsdal 13.12.2005 */

/* Startup settings for each program */
$program_name = "PHP Classifieds";
$header_file = "adm_header.php";
$header_html="<table border='0' class='bstyle' cellspacing='0' width='100%'><tr><td>";
$footer_html="</td></tr></table>";
$footer_file = "adm_footer.php";
$current_editing_file = "gen_inc.php";


/* Default variables */
$override= 1;
$error=0;
if (!$set_language)
	$set_language="eng";
	
if (!$set_path)
{
	$set_path = getcwd();
	$set_path=ereg_replace("admin","",$set_path);
}


include ("$header_file");
if (!WA())
	$set_demo=1;

$files["gen_inc.php"] = LA_SET_GEN_SETTINGS;
$files["opt_inc.php"] = LA_SET_OPT_SETTINGS;
$files["pay_inc.php"] = LA_SET_PAY_SETTINGS;
$files["board_inc.php"] = LA_SET_BOARD_SETTINGS;


writeCommonAdminTable("Settings",1);
include ("adm_set_fill.php");

echo $header_html;

foreach ($files as $key => $value) {
	
	echo "<a href='?file=$key'><b>$value</b></a>&nbsp;&nbsp;";
	
	if (isset($_REQUEST["file"]))
	{
		if ($_REQUEST["file"] == $key)
		{
			$current_editing_file = $key;	
		}
		
	}
	
}


if (!file_exists("config/$current_editing_file"))
{
	
	
	if (getParam("searchWord","")=="")
	{
		$res = fopen("config/$current_editing_file","w");
		$writeres = fwrite($res,"");
		@chmod("config/$current_editing_file",octdec($set_chmod));
	}
	
}

//echo "<h1>$program_name</h1>";
echo "<h3>".$files[$current_editing_file]."</h3>";




function getItems($aType,$aFieldName,$aFieldValue,$aX,$aY,$default)
{	
	if ($aType == "c")
	{
		$tmp_value = split(",", $GLOBALS["$aFieldName"]);
		$aFieldValue = trim($aFieldValue);
		echo "<input type='checkbox' name='$aFieldName" . "[]" . "' value='$aFieldValue'";
		if (in_array($aFieldValue,$tmp_value))
			echo " checked";
		
		echo ">\n";
	}
	elseif ($aType=="t")
	{
		$aFieldValue = trim($GLOBALS["$aFieldName"]);
		
		if (Empty($aFieldValue))
			$aFieldValue = $default;
		echo "<input type='text' name='$aFieldName' value='$aFieldValue' size='$aX'>\n";				
	}
	elseif ($aType=="p")
	{
		$aFieldValue = trim($GLOBALS["$aFieldName"]);
		
		if (Empty($aFieldValue))
			$aFieldValue = $default;
		echo "<input type='password' name='$aFieldName' value='$aFieldValue' size='$aX'>\n";				
	}
	elseif ($aType == "r")
	{
		$default = trim($default);
		$va = $GLOBALS["$aFieldName"];
		if (is_array($va))
			$val = trim($va[0]);
		else 
			$val = trim($va);
		$tmp_value = $val;
		$aFieldValue = trim($aFieldValue);
		
		echo "<input type='radio' name='$aFieldName" . "[]" . "' value='$aFieldValue'";
		if ($aFieldValue===$tmp_value)
			echo " checked";
		elseif (!$tmp_value AND $default==$aFieldValue)
				echo " checked";
		echo ">\n";
		
		
	}
	
	elseif ($aType=="ta")
	{
		$aFieldValue = trim($GLOBALS["$aFieldName"]);
		if (Empty($aFieldValue))
			$aFieldValue = $default;
		echo "<textarea name='$aFieldName' cols='$aX' rows='$aY'>$aFieldValue</textarea>\n";				
	}
	
	
}



if (isset($_REQUEST["submit"]) AND getParam("searchWord","")=="")
{
	$str = "<?";
	foreach ($_POST as $key => $value) 
	{
		$key = trim($key);
		
		if ($key<>"submit" && $key<>"file")
		{
			if (is_array($value))
			{
				$opt = "";
				foreach ($value as $element)
				{
					$opt = $opt . $element . ",";
				}
				$opt = substr($opt,0,-1);
				$str =  $str . "$". "$key = \"".trim($opt)."\"" . ";\n";
			}
			else 
			{
				$str =  $str . "$". "$key = \"".trim($value)."\"" . ";\n";
			}	
				
		}
	}
	$str = $str . "?>";
	if ($set_demo)
	{
		echo "<b>Demo-mode</b><br>System in demo-mode, no configuration saved!<br>";	
	}
	else 
	{
	$r = fopen("config/$current_editing_file","w");
	$write = fwrite($r,$str);
	@chmod("config/$current_editing_file",octdec($set_chmod));
	}

}
require ("config/$current_editing_file");  



if (file_exists("setup/configuration_src/$current_editing_file.txt"))
	$options = file("setup/configuration_src/$current_editing_file.txt");
else
{ 
	$options = "";
	echo "<p><font color='red'><b>Error:</b></font><br>Question file setup/configuration_src/$current_editing_file.txt not found.</p>";	
	$error = 1;
}

if (!$error)
{
	echo "<form method='post' action='adm_set.php'>";
	echo "<table width='100%' class='noBorder' border='0'><tr><td valign='top'><input type='submit' name='submit' value='".LA_SET_GEN_SAVE."'></td><td valign='top'>";
	echo "<input type='text' name='searchWord' value='" .  $_REQUEST['searchWord']  . "'>";
	echo "&nbsp;<input type='submit' name='submit' value='".LA_SET_GEN_SEARCH."'>";
	echo "</td></tr></table><p>";
	
}

if (!empty($_REQUEST["searchWord"]))
{
	
	echo "<table width='100%' cellpadding='3' cellspacing='0'><tr><td bgcolor='lightgrey'><b>".LA_SET_GEN_SEARCH."</b></td></tr></table>";
	
	foreach ($files as $key => $value) 
	{
		$found=0;
		echo "<p><b>" . $files[$key] . "</b></p><ul type='square'>";
	
		$options=file("setup/configuration_src/" . $key . ".txt");
		
		foreach ($options as $line)
		{
			
			$line=trim($line);
			
			if (!empty($line))
			{
				$tmp_split = split("\|",$line);
				$type = $tmp_split[0];	
				$varname = trim($tmp_split[1]);
				$title = $tmp_split[2];
				$question = $tmp_split[3];
				$lSearchWord=$_REQUEST["searchWord"];
		
				if ((eregi($lSearchWord,$title) || eregi($lSearchWord,$varname) || eregi($lSearchWord,$question) AND !empty($title)))
				{
					echo "<li><a href='adm_set.php?file=$key#$varname'>$title</a></li>";
					$found=1;
				}
			}
		}
		
		if ($found==0)
			echo "<li>No match</li>";
		
		echo "</ul>";
	}
}
else 
{

foreach ($options as $line)
{
	$tmp_split = split("\|",$line);
	$type = $tmp_split[0];	
	$varname = trim($tmp_split[1]);
	$title = $tmp_split[2];
	$question = $tmp_split[3];
	$tmp_options = $tmp_split[4];
	$tmp_options_val = $tmp_split[5];
	$x = $tmp_split[6];
	$y = $tmp_split[7];
	$default = $tmp_split[8];
	$grab_fill_var = $tmp_split[9];
	$image = $tmp_split[10];
	$options = split(",",$tmp_options);
	$options_val = split(",",$tmp_options_val);
	$num_options = count($options);
	$line=trim($line);
	
	if (($lVarName=="set_mysql_pass" | $lVarName=="set_mysql_user") && !gw())
		$lSkip=1;
	
	if ($type==":::")
	{
		$lData = call_user_func($varname);	
		
		echo "<table width='100%' cellpadding='3' cellspacing='0'><tr><td bgcolor='lightgrey'><b>".LA_SET_PAYMENT_VENDORS."</b></td></tr></table><br>";
		foreach ($lData as $lItem)
		{
			echo "<b><a name='set_pay_account_".$lItem["id"]."' style='color:black'>".$lItem["name"]."</a></b> $img";
			echo "<br>".LA_SET_PAYMENT_VENDORS_ACCOUNT."<br />";			
			echo getItems("t","set_pay_account_" . $lItem["id"],"",$x,$y,$default) . "";
			echo "<br>".LA_SET_PAYMENT_VENDORS_CURRENCY."<br />";	
			echo getItems("t","set_pay_currency_" . $lItem["id"],"",$x,$y,$default) . "<br /><br />";
			echo "<br>".LA_SET_PAYMENT_VENDORS_FEE."<br />";	
			echo getItems("t","set_pay_fee_" . $lItem["id"],"",$x,$y,$default) . "<br /><br />";
			
		}
		//$lSkip=1;
	}
	
	
	$lTempTitle="LA_" . strtoupper($varname) . "_TITLE";
	$lTempDescr="LA_" . strtoupper($varname) . "_DESCR";
		
	if (@constant($lTempTitle)!="")
		$lSettingName=constant($lTempTitle);
	if (@constant($lTempDescr)!="")
		$lSettingDescription=constant($lTempDescr);
		
	
	if (!empty($line) && !$lSkip)
	{
	
	$img="";
	
	if ($type=="a")
	{
		echo "<table width='100%' cellpadding='3' cellspacing='0'><tr><td bgcolor='lightgrey'><b>".($varname)."</b></td></tr></table><br>";
		//LA_SET_CAPTION_MYSQL_CONNECTION
		//$lMainCaption="LA_SET_CAPTION_" . strtoupper($varname);
		
		
	}
	elseif (!$lSkip) {
		
	if (trim($image) !="")
  	{
  		//$lRet=setImageSize($image,60,1);
  		
  		//print_r($lRet);
		$img="<img align='right' valign='top' src='../layout_images/pay_vendors/$image' />";
  	}	

  	
  	//$sFileContent .= "define(".var_export($lTempTitle, true).",".var_export(makeLineBreaksFromCRLF(htmlentities($title)), true).");\n";
  	//$sFileContent .= "define(".var_export($lTempDescr, true).",".var_export(makeLineBreaksFromCRLF(htmlentities($question)), true).");\n";
	echo "<b><a name='$varname' style='color:black'>$lSettingName</a></b> $img<br>";
	echo $lSettingDescription . "<br>";
	
	$grab_fill_var=trim($grab_fill_var);
	
	if (!empty($grab_fill_var))
	{
		$get_data = call_user_func(trim($grab_fill_var));	
		$options = $get_data;
		$num_options = count($options);
		
	}
	
		
	
	for ($i=0; $i<$num_options; $i++)
 	{
 		if ($grab_fill_var)
 		{
 			$tmp_sp = split(",", $options[$i]);
 			$value = $tmp_sp[0];
 			$descr = $tmp_sp[1];	
 			if (!is_array($options))
 				$default=$options;
 			echo getItems($type,$varname,$value,$x,$y,$default);	
 			echo $descr . "&nbsp;&nbsp;";
 		}
 		else
 		{ 
 			$value = $options_val[$i];
 			echo getItems($type,$varname,$value,$x,$y,$default);	
 			echo $options[$i] . "&nbsp;&nbsp;";
 		}
  	}
  	
	echo "<p>&nbsp;</p>";	
	}}
	
	 
	
}
//echo $sFileContent;
if (!$error)
{
	echo "<input type='hidden' name='file' value='$current_editing_file'>";
	echo "<input type='submit' name='submit' value='".LA_SET_GEN_SAVE."'>";
}
}
echo "</form>";
writeCommonAdminTable("Import Tool","");
echo $footer_html;
include ("$footer_file");

?>